//
//  MSTabBarView.m
//  MyShare
//
//  Created by koki on 15/9/15.
//  Copyright (c) 2015年 MyShare. All rights reserved.
//

#import "MSTabBarView.h"

#define CELL_TabBar @"CELL_TabBar"

@interface MSTabBarView ()
@property MSTableBarCell* curCell;
@property UICollectionView* collectView;
@end

@implementation MSTabBarView
@synthesize collectView;

-(id)initWithFrame:(CGRect)frame
{
    self=[super initWithFrame:frame];
    if (self)
    {
        UICollectionViewFlowLayout* flowLayout=[[UICollectionViewFlowLayout alloc] init];
        flowLayout.itemSize=CGSizeMake(self.width/3.0,KTabBarHeight);
        flowLayout.minimumInteritemSpacing=0;
        flowLayout.minimumInteritemSpacing=0;
        
        collectView=[[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:flowLayout];
        collectView.delegate=self;
        collectView.dataSource = self;
        collectView.bounces=NO;
        collectView.scrollsToTop = NO;
        collectView.backgroundColor = [UIColor clearColor];
        [self addSubview:collectView];
        
        [self.collectView registerClass:[MSTableBarCell class] forCellWithReuseIdentifier:CELL_TabBar];
        CGRect rect=CGRectMake(0,0,self.width, 0.5);
        UIImageView *line = [[UIImageView alloc] initWithFrame:rect];
        line.image = [UIImage imageNamed:@"dot.png"];
        [self addSubview:line];
    }
    return self;
}

-(void)setDataArray:(NSMutableArray *)dataArray
{
    _dataArray=nil;
    _dataArray=[[NSMutableArray alloc] initWithArray:dataArray];
    [self.collectView reloadData];
}

#pragma mark -
#pragma mark UICollectionView Data Source

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MSTableBarCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:CELL_TabBar forIndexPath:indexPath];
    cell.data=self.dataArray[indexPath.item];
    cell.bSeleted=NO;
    if (self.selectedIndex==0 && indexPath.row==0)
    {
        cell.bSeleted=YES;
        self.curCell=cell;
    }
    
    if(self.selectedIndex && self.selectedIndex==indexPath.row)
    {
        cell.bSeleted=YES;
    }
    
    return cell;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

-(UICollectionViewController*)nextViewControllerAtPoint:(CGPoint)p
{
    return nil;
}

#pragma mark -
#pragma mark UICollectionView Delegate

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    MSTableBarCell* cell=(MSTableBarCell*)[collectionView cellForItemAtIndexPath:indexPath];    
    if (_selectedIndex!=indexPath.row)
    {
        self.curCell=cell;
        cell.bSeleted=YES;
        _selectedIndex=(int)indexPath.row;
        if (self.delegate && [self.delegate respondsToSelector:@selector(tabBarView:didSelectItemIndex:)])
        {
            [self.delegate tabBarView:self didSelectItemIndex:_selectedIndex];
        }
        [collectionView reloadData];
    }
}

-(void)setSelectedIndex:(int)selectedIndex
{
    NSIndexPath * indexPath=[NSIndexPath indexPathForRow:selectedIndex inSection:0];
    [self collectionView:self.collectView didSelectItemAtIndexPath:indexPath];
    _selectedIndex=selectedIndex;
}


@end


//**** realize MSTableBarCell ****************

@interface MSTableBarCell ()
@property (nonatomic, strong)GXButton* btn;
@end

@implementation MSTableBarCell
@synthesize btn;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.contentView.backgroundColor = [UIColor colorWithWhite:1 alpha:0.95];
        self.btn = [GXButton buttonWithType:UIButtonTypeCustom];
        btn.relayout=YES;
        btn.frame=CGRectMake(0, 0, self.width, self.height);
        btn.imageViewFrame=CGRectMake((self.width-26)/2.0, 6, 25, 25);
        btn.titleLabelFrame=CGRectMake(0,34, self.width,15);
        btn.titleLabel.font=[UIFont fontWithName:@"STHeitiSC-Light" size:11];
        btn.titleLabel.textAlignment=NSTextAlignmentCenter;
        btn.userInteractionEnabled=NO;
        [btn setTitleColor:kLightBlackColor forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor blueColor] forState:UIControlStateSelected];
        [self.contentView addSubview:btn];
    }
    return self;
}

-(void)setData:(NSDictionary *)data
{
    _data=data;
    NSString* name=data[@"name"];
    btn.titleLabelFrame=CGRectMake(0,34, self.width,14);
    btn.titleLabel.font=[UIFont fontWithName:@"STHeitiSC-Light" size:10];
    [btn setTitle:name forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:data[@"commonIcon"]] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:data[@"selectedIcon"]] forState:UIControlStateSelected];
}

-(void)setBSeleted:(BOOL)bSeleted
{
    _bSeleted=bSeleted;
    btn.selected=bSeleted;
}

@end

